package com.example.template;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.templates.DirectiveHandler;
import com.example.common.templates.TemplateDirective;
import com.example.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author fangyu
 * @version v1.0.0
 * @date 2020/7/21 3:52 下午
 */
@Component
public class PostsTemplate extends TemplateDirective {

    @Autowired
    PostService postService;

    @Override
    public String getName() {
        return "posts";
    }

    @Override
    public void execute(DirectiveHandler handler) throws Exception {
        Integer level = handler.getInteger("level");
        Integer page = handler.getInteger("page", 1);
        Integer size = handler.getInteger("size", 2);
        Long categoryId = handler.getLong("categoryId");

        IPage p = postService.paging(new Page(page, size), categoryId, null, level, null, "created");
        System.out.println(p.getRecords());
        handler.put(RESULT, p).render();
    }
}
